package com.example.distinguish_server.pattern.Singleton;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.toolkit.ImageInfo;

import java.util.ArrayList;
import java.util.List;

public class FaceEngineSing {
    private static FaceEngineSing instance;
    private FaceEngine faceEngine;
    private FaceEngineSing() {
        // 私有构造方法，防止外部实例化
        run();
    }

    public static synchronized FaceEngineSing getInstance() {
        if (instance == null ) {
            instance = new FaceEngineSing();
        }
        return instance;
    }
    public FaceEngine run(){
        //从官网获取
        String appId = "G461Zsj34BHah1shR47jgSBP31wW4EH2vSFQvTV828Ko";
        String sdkKey = "HFroosvwnrgFgpbZU4ARkBG6d43ryLj6szoaNiKraS7d";


        faceEngine = new FaceEngine("D:\\JAVA\\textDemo\\libs\\WIN64");
        //激活引擎
        int errorCode = faceEngine.activeOnline(appId, sdkKey);

        if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            System.out.println("引擎激活失败");
        }


        ActiveFileInfo activeFileInfo=new ActiveFileInfo();
        errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
        if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            System.out.println("获取激活文件信息失败");
        }

        //引擎配置
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
        engineConfiguration.setDetectFaceMaxNum(10);
        engineConfiguration.setDetectFaceScaleVal(16);
        //功能配置
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        functionConfiguration.setSupportAge(true);
        functionConfiguration.setSupportFace3dAngle(true);
        functionConfiguration.setSupportFaceDetect(true);
        functionConfiguration.setSupportFaceRecognition(true);
        functionConfiguration.setSupportGender(true);
        functionConfiguration.setSupportLiveness(true);
        functionConfiguration.setSupportIRLiveness(true);
        engineConfiguration.setFunctionConfiguration(functionConfiguration);


        //年龄
        errorCode = faceEngine.init(engineConfiguration);

        if (errorCode != ErrorInfo.MOK.getValue()) {
            System.out.println("初始化引擎失败");
            faceEngine = null;
        } else {
            System.out.println("初始化引擎成功");
        }
        return faceEngine;
    }

    /**
     * 人脸检测
     * @param imageInfo2
     * @return
     */
    public List<FaceInfo> getDetectFaces(ImageInfo imageInfo2) {
        List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>();
        int errorCode = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(),imageInfo2.getImageFormat(), faceInfoList2);
        return faceInfoList2;
    }

    /**
     *特征提取
     * @param imageInfo
     * @param faceInfoList 人脸检测
     * @return
     */
    public FaceFeature getExtractFaceFeature(ImageInfo imageInfo,List<FaceInfo> faceInfoList){
        FaceFeature faceFeature = new FaceFeature();
        int errorCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList.get(0), faceFeature);
        return faceFeature;

    }

    /**
     * r比对人脸特征数据获取相似度
     * @param faceFeature 特征提取
     * @param faceFeature2 特征提取
     * @return
     */
    public FaceSimilar compareFace(byte[] faceFeature, byte[] faceFeature2){
        FaceFeature targetFaceFeature = new FaceFeature();
        targetFaceFeature.setFeatureData(faceFeature);
        FaceFeature sourceFaceFeature = new FaceFeature();
        sourceFaceFeature.setFeatureData(faceFeature2);
        FaceSimilar faceSimilar = new FaceSimilar();
        int errorCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
        return faceSimilar;
    }
}
